﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;

namespace AssetAllocationSystem.Repositories
{
    public class RepositoryBase<T>
    {
        private ISession session;

        public RepositoryBase(ISession session)
        {
            this.session = session;
        }

        public bool CreateOrUpdate(T entity)
        {
            session.SaveOrUpdate(entity);
            session.Flush();

            return true;
        }

        public bool Delete(T entity)
        {
            session.Delete(entity);
            session.Flush();

            return true;
        }

        public IList<T> FindAll()
        {
            var asset = DetachedCriteria
                .For<T>()
                .GetExecutableCriteria(session)
                .List<T>();

            return asset;
        }

        public T FindById(Guid id)
        {
            var entity = DetachedCriteria
                .For<T>()
                .Add(Restrictions.Eq("Id", id))
                .GetExecutableCriteria(session)
                .UniqueResult<T>();

            return entity;
        }

        // Move the FindById() from all the entities to the RepositoryBase

        //public T FindById(Guid id)
        //{
            
        //}
    }
}